package com.cn.demo5;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

public class DemoPathTwo {
    public void printClassPath(){
        File file = new File(this.getClass().getResource("/").getPath());
        System.out.println("类路径跟目录为: "+ file.getAbsolutePath());

        File current = new File(this.getClass().getResource("").getPath());
        System.out.println("当前类所在路径为: "+current.getAbsolutePath());
    }

    public void loadProperties(){
        try(InputStream in = this.getClass().getResourceAsStream("/config.properties")){
            if (in == null) {
                System.out.println("未找到 config.properties 文件！");
                return;
            }
            Properties properties = new Properties();
            properties.load(in);
            System.out.println("读到的配置如下：");
            System.out.println("app.name = " + properties.getProperty("app.name"));
            System.out.println("app.version = "+properties.getProperty("app.version"));
        }catch (Exception e){
            System.err.println("读取配置文件出错：" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        DemoPathTwo demoPathTwo = new DemoPathTwo();
        demoPathTwo.printClassPath();
        demoPathTwo.loadProperties();
    }

}
